const mapUtils = require('../../libs/mapsdk.js');
Page({
  data: {
    statusBarHeight: 0,
    activeTab: 'rescue',  // 'rescue' 或 'repair'
    repairShops: [
    ],
    rescuePhones: [
      { name: '中国道路救援服务', phone: '12122' },
      { name: '全国道路救援热线', phone: '4008-199-199' },
      { name: '北京市道路救援', phone: '4000-168-168' }
    ],
    troubleList: [
      {
        title: '车辆无法启动',
        solutions: [
          '检查电瓶连接是否牢固',
          '检查电瓶电压是否正常',
          '检查启动机和点火开关',
          '检查燃油系统是否正常'
        ]
      },
      {
        title: '轮胎爆胎/漏气',
        solutions: [
          '将车辆停在安全位置',
          '打开危险警示灯',
          '使用千斤顶抬起车辆',
          '更换备胎或使用补胎工具'
        ]
      },
      {
        title: '发动机过热',
        solutions: [
          '将车辆停在安全位置并关闭发动机',
          '等待发动机冷却后检查冷却液',
          '检查水箱是否漏水',
          '检查风扇是否正常工作'
        ]
      },
      {
        title: '刹车失灵',
        solutions: [
          '尝试泵刹车踏板',
          '降低车速并使用手刹',
          '谨慎寻找安全地点停车',
          '拨打救援电话寻求帮助'
        ]
      }
    ]
  },

  onLoad: function () {
    // 获取状态栏高度
    const systemInfo = wx.getSystemInfoSync();
    this.setData({
      statusBarHeight: systemInfo.statusBarHeight
    });
    mapUtils.searchNearby('道路救援',res =>{
      this.setData({
        repairShops:res
      })
    });
  },

  // 切换标签页
  switchTab: function (e) {
    const tab = e.currentTarget.dataset.tab;
    this.setData({
      activeTab: tab
    });
  },

  // 一键呼叫救援
  callRescue: function (e) {
    const phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone
    });
  },

  // 拨打修理厂电话
  callRepairShop: function (e) {
    const phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone
    });
  },

  // 导航到修理厂
  navigateToShop: function (e) {
    const shopId = e.currentTarget.dataset.id;
    const shop = this.data.repairShops.find(item => item.id === shopId);
    console.log(shop);
    // 真实环境下使用以下代码导航
    wx.openLocation({
      latitude: shop.location.lat,
      longitude: shop.location.lng,
      name: shop.title,
      address: shop.address,
      scale: 18
    });
  },

  // 返回上一页
  handleBack: function () {
    wx.navigateBack();
  },

  // 查看故障详情
  showTroubleDetail: function (e) {
    const index = e.currentTarget.dataset.index;
    const trouble = this.data.troubleList[index];

    wx.showModal({
      title: trouble.title,
      content: trouble.solutions.join('\n\n'),
      showCancel: false,
      confirmText: '我知道了'
    });
  }
}) 